package com.springboot.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.springboot.entity.OutParm;
import com.springboot.entity.TbRisReport;
import com.springboot.entity.Zthc;
import com.springboot.hisdao.HisMapper;
import com.springboot.risdao.RisMapper;
import com.springboot.util.DateTimeUtil;
import com.springboot.util.OutParmUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.emrdao.EmrMapper;
import com.springboot.service.IHisLisEmrService;

@Service("hisLisEmrService")
public class HisLisEmrServiceImp implements IHisLisEmrService {
	@Autowired
	private HisMapper hisMapper;
	@Autowired
	private EmrMapper emrMapper;

	@Autowired
	private RisMapper risMapper;


	@Override
	public OutParm RIS_ApplyInfo(Zthc zthc) {

		Map<String,Object> returnmap = new HashMap<>();


		List<Map<String,Object>> list =  hisMapper.getJcsqxxcx(zthc);
		returnmap.put("total",list.size());
		returnmap.put("pages",1);
		returnmap.put("list",list);

		return OutParmUtil.success("成功",returnmap);
	}

	@Override
	public OutParm getJcsqxxmxcx(Zthc zthc) {
		Map<String,Object> returnmap = new HashMap<>();

		List<Map<String,Object>> list =  hisMapper.getJcsqxxmxcx(zthc);

		returnmap.put("total",list.size());
		returnmap.put("pages",1);
		returnmap.put("list",list);

		return OutParmUtil.success("成功",returnmap);
	}

	@Override
	public OutParm RIS_ReportState(Zthc zthc) {

		int mzxx = hisMapper.getMzxx(zthc);

		if(mzxx>0){
			zthc.setHzlx("1");
		}else{
			int zyxx = hisMapper.getZyxx(zthc);
			if(zyxx>0){
				zthc.setHzlx("2");
			}else{
				int tjxx = hisMapper.getTjxx(zthc);
				if(tjxx>0){
					zthc.setHzlx("3");
				}else{
					return OutParmUtil.fail("检查申请信息不存在或已经回写!");
				}
			}
		}

		for (int i = 0; i < zthc.getSqdid().size(); i++) {

			zthc.setSqdmxid(zthc.getSqdid().get(i).getSqdmxid());

			hisMapper.updateJczthc(zthc);
		}

		Map<String,Object> returnmap = new HashMap<>();

		returnmap.put("YLJGDM",zthc.getYljgbm());
		returnmap.put("SQDH",zthc.getSqdh());
		returnmap.put("CZZT",zthc.getCzzt());
		returnmap.put("CZRQ", DateTimeUtil.getChar14En(zthc.getCzrq()));
		returnmap.put("CZYBM",zthc.getCzybm());
		returnmap.put("CZYXM",zthc.getCzyxm());

		return OutParmUtil.success("成功",returnmap);
	}

	@Override
	public OutParm doPay(Zthc zthc) {

		String msg = "";
		if ("1".equals(zthc.getKtfbz()))
		{
			zthc.setYxbz("1");
			zthc.setKfzt("0");
			zthc.setBzsm("医技登记扣费");
			msg = "收费成功";
		}
		else if ("2".equals(zthc.getKtfbz()))
		{
			zthc.setYxbz("0");
			zthc.setKfzt("1");
			zthc.setBzsm("医技撤消退费");
			msg = "退费成功";
		}
		else
		{
			return OutParmUtil.fail("扣退费标志只能是1或2，请检查！");
		}

		for (int i = 0; i < zthc.getSqdid().size(); i++) {

			zthc.setSqdmxid(zthc.getSqdid().get(i).getSqdmxid());

			hisMapper.pacsKf(zthc);
		}

		return OutParmUtil.success(msg,null);
	}

	@Override
	public OutParm RIS_ADD_Report(TbRisReport tbRisReport) {

		return OutParmUtil.success("成功",risMapper.insertTbRisReport(tbRisReport));
	}
}
